<template>  
  <div class="system-ys7CamerasSchedule-edit">
    <!-- 添加或修改萤石云摄像头抓图时间模板对话框 -->
    <el-dialog v-model="isShowDialog" width="769px" :close-on-click-modal="false" :destroy-on-close="true">
      <template #header>
        <div v-drag="['.system-ys7CamerasSchedule-edit .el-dialog', '.system-ys7CamerasSchedule-edit .el-dialog__header']">{{(!formData.id || formData.id==0?'添加':'修改')+'萤石云摄像头抓图时间模板'}}</div>
      </template>
      <el-form ref="formRef" :model="formData" :rules="rules" label-width="120px">        
        <el-form-item label="名称" prop="name">
          <el-input v-model="formData.name" placeholder="请输入名称" />
        </el-form-item>        
        <el-form-item label="时间表(小时)" prop="hours">
          <el-checkbox-group v-model="formData.hours">
          <el-row>
            <el-col v-for="(item,i) of 24" :key="i" :span="4">
              <el-checkbox :label="i+'时'"  />
            </el-col>
          </el-row>
        </el-checkbox-group>
        </el-form-item>        
        <el-form-item label="时间表(分钟)" prop="minutes">
          <el-checkbox-group v-model="formData.minutes">
            <el-row>
            <el-col v-for="(item,i) of 6" :key="i"  :span="4" >
              <el-checkbox :label="(i*10)+'分'" />
            </el-col>
          </el-row>
        </el-checkbox-group>
        </el-form-item>      
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="onSubmit">确 定</el-button>
          <el-button @click="onCancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
<script lang="ts">
import { reactive, toRefs, defineComponent,ref,unref,getCurrentInstance } from 'vue';
import {ElMessageBox, ElMessage, FormInstance,UploadProps} from 'element-plus';
import {
  getYs7CamerasSchedule,
  addYs7CamerasSchedule,
  updateYs7CamerasSchedule,  
} from "/@/api/system/ys7CamerasSchedule";
import {
  Ys7CamerasScheduleInfoData,
  Ys7CamerasScheduleEditState,  
} from "/@/views/system/ys7CamerasSchedule/list/component/model"
export default defineComponent({
  name:"index",
  setup(props,{emit}) {    
    const {proxy} = <any>getCurrentInstance()
    const formRef = ref<HTMLElement | null>(null);
    const menuRef = ref();    
    const state = reactive<Ys7CamerasScheduleEditState>({
      loading:false,
      isShowDialog: false,
      formData: {        
        id: undefined,        
        name: undefined,        
        hours: undefined,        
        minutes: undefined,        
      },
      // 表单校验
      rules: {        
        id : [
            { required: true, message: "序号不能为空", trigger: "blur" }
        ],        
        name : [
            { required: true, message: "名称不能为空", trigger: "blur" }
        ],        
      }
    });
    // 打开弹窗
    const openDialog = (row?: Ys7CamerasScheduleInfoData) => {
      resetForm();
      if(row) {        
        getYs7CamerasSchedule(row.id!).then((res:any)=>{
          const data = res.data;          
          state.formData = data;
          state.formData.hours = JSON.parse(data.hours);
          state.formData.hours=state.formData.hours.map(element => {
            return element+'时'
          });
          state.formData.minutes = JSON.parse(data.minutes);
          state.formData.minutes=state.formData.minutes.map(element => {
            return element+'分'
          });
          
      })
    }
      state.isShowDialog = true;
    };
    // 关闭弹窗
    const closeDialog = () => {
      state.isShowDialog = false;
    };
    // 取消
    const onCancel = () => {
      closeDialog();
    };
    // 提交
    const onSubmit = () => {
      const formWrap = unref(formRef) as any;
      if (!formWrap) return;
      formWrap.validate((valid: boolean) => {
        if (valid) {
          state.loading = true;
          if(!state.formData.id || state.formData.id===0){
            //添加
          addYs7CamerasSchedule(state.formData).then(()=>{
              ElMessage.success('添加成功');
              closeDialog(); // 关闭弹窗
              emit('ys7CamerasScheduleList')
            }).finally(()=>{
              state.loading = false;
            })
          }else{
            //修改
            let hours=state.formData.hours.map(item=>{
        return parseInt(item)
      })
      let minutes=state.formData.minutes.map(item=>{
        return parseInt(item)
      })
      let obj={
        name:state.formData.name,hours,minutes,id:state.formData.id
      }
          updateYs7CamerasSchedule(obj).then(()=>{
              ElMessage.success('修改成功');
              closeDialog(); // 关闭弹窗
              emit('ys7CamerasScheduleList')
            }).finally(()=>{
              state.loading = false;
            })
          }
        }
      });
    };
    const resetForm = ()=>{
      state.formData = {        
        id: undefined,        
        name: undefined,        
        hours: undefined,        
        minutes: undefined,        
      }
    };    
    return {
      proxy,
      openDialog,
      closeDialog,
      onCancel,
      onSubmit,
      menuRef,
      formRef,      
      ...toRefs(state),
    };
  }
})
</script>
<style scoped>  
.system-ys7CamerasSchedule-edit{
  .el-checkbox-group{
    width: 100% !important;
  }
}
</style>